package com.maven.trans.demos.controller;


import com.maven.trans.demos.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    // 事务管理器
    @Autowired
    DataSourceTransactionManager transactionManager;
    // 定义事务的属性
    @Autowired
    TransactionDefinition transactionDefinition;

    @Autowired
    UserService userService;

    @RequestMapping("/register")
    public String register(String userName, String password) {
        // 编程式 事务 ： 开启事务
        TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition);

        Integer result = userService.register(userName,password);
        log.info("注册成功，影响行数："+result);

        // 提交事务
        transactionManager.commit(transaction);
/*        // 回滚事务
        transactionManager.rollback(transaction);*/
        return "success";
    }
}
